﻿#region [===== Using =====]
using System;
using System.Collections;
using System.Collections.Generic;
using Microsoft.SharePoint;
#endregion

namespace TPG.SharePoint.Workflow
{
    /// <summary>
    /// Provides a basic collection of <see cref="TaskItem" /> objects.
    /// </summary>
    public class TaskItemCollection 
        : IEnumerable<TaskItem>
    {
        #region [===== Fields =====]
        List<TaskItem> _innerList;
        #endregion

        #region [===== Properties =====]
        /// <summary>
        /// Gets the <see cref="SPContentType" /> assigned to the task 
        /// list-items.
        /// </summary>
        /// <remarks>
        /// This value serves as the default when creating new 
        /// items using the <see cref="TaskItemCollection.NewTaskItem(int)" /> 
        /// method.
        /// </remarks>
        public SPContentType TaskContentType { get; private set; }
        /// <summary>
        /// Gets or sets a value indicating if random keys are allowed to 
        /// access task-data.
        /// </summary>
        /// <remarks>
        /// This value serves as the default when creating new 
        /// items using the <see cref="TaskItemCollection.NewTaskItem(int)" /> 
        /// method.
        /// </remarks>
        public bool AllowExtendedProperties { get; set; }

        /// <summary>
        /// Gets or sets a <see cref="TaskItem" /> from the 
        /// collection.
        /// </summary>
        /// <param name="index">The index of the item.</param>
        /// <returns>The TaskItem.</returns>
        public TaskItem this[int index]
        {
            get { return _innerList[index]; }
            set { _innerList[index] = value; }
        }
        /// <summary>
        /// Gets the number of task items contained in the collection.
        /// </summary>
        public int Count
        {
            get { return _innerList.Count; }
        }
        #endregion

        #region [===== Constructors =====]
        /// <summary>
        /// Initializes a new instance of the <see cref="TaskItemCollection" /> class.
        /// </summary>
        /// <param name="taskContentType">The content-type used by the 
        /// task list items.</param>
        public TaskItemCollection(SPContentType taskContentType)
        {
            _innerList = new List<TaskItem>();
            TaskContentType = taskContentType;
        }
        #endregion

        #region [===== Public Methods =====]
        /// <summary>
        /// Creates a new empty <see cref="TaskItem" /> using 
        /// a specific list item ID.
        /// </summary>
        /// <param name="itemID">The ID of the list item containing the task 
        /// data.</param>
        /// <returns>The new empty TaskItem.</returns>
        public TaskItem NewTaskItem(int itemID)
        {
            TaskItem item = new TaskItem(itemID,
                TaskContentType);
            item.AllowExtendedProperties = AllowExtendedProperties;
            Add(item);
            return item;
        }

        /// <summary>
        /// Gets an object that can be used to enumerate over the items 
        /// contained in this collection.
        /// </summary>
        /// <returns>An enumerator.</returns>
        public IEnumerator<TaskItem> GetEnumerator()
        {
            return _innerList.GetEnumerator();
        }

        /// <summary>
        /// Gets an object that can be used to enumerate over the items 
        /// contained in this collection.
        /// </summary>
        /// <returns>An enumerator.</returns>
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return ((IEnumerable<TaskItem>)this).GetEnumerator();
        }
        #endregion

        #region [===== Internal methods =====]
        internal void Add(TaskItem item)
        {
            _innerList.Add(item);
        }

        internal TaskItem NewTaskItem(int id, string displayName,
            SPListItem listItem, Hashtable extendedProperties)
        {
            TaskItem item = new TaskItem(
                id, displayName, listItem, extendedProperties);
            item.AllowExtendedProperties = AllowExtendedProperties;
            Add(item);
            return item;
        }
        #endregion
    }
}
